/*
 * Created by JFormDesigner on Tue Jul 02 11:07:46 CST 2024
 */

package scct.yzx.ui;

import scct.yzx.dao.ContactDao;
import scct.yzx.dao.GroupDao;
import scct.yzx.dao.UserDao;
import scct.yzx.model.Contact;
import scct.yzx.model.Group;
import scct.yzx.model.User;
import scct.yzx.utils.Tools;

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import javax.swing.*;

/**
 * @author lenovo
 */
public class DlgGroupUpdate extends JDialog {
    int id;
    public DlgGroupUpdate(Window owner,int id) {
        super(owner);//调用父类
        this.id=id;//传入的id值赋给当前对象的id属性
        initComponents();//初始化对话框中的组件
        initView();
    }
//    初始化视图组件
//    将数据库中的所有用户添加到一个下拉列表框中。

    private void initView() {
        UserDao userDao = new UserDao();
        List<User> allUser = userDao.getAllUser();
        for (User user:allUser) {
            comCreator.addItem(user.getUsername());
        }

        GroupDao groupDao=new GroupDao();
        Group group=groupDao.getGroupId(id);
        txtGroupName.setText(group.getGroupname());


    }

    private void btnUpdate(ActionEvent e) {
        // TODO add your code here
        String groupname=txtGroupName.getText();
        String creatorname= (String) comCreator.getSelectedItem();
        String createtime= Tools.getCreatetime();
        GroupDao groupDao=new GroupDao();
        if (groupDao.updateGroup(id,groupname,creatorname,createtime)){
            JOptionPane.showMessageDialog(this,"修改成功");
            dispose();
        }
    }

    private void btnCancle(ActionEvent e) {
        // TODO add your code here
        this.dispose();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - yzx
        label1 = new JLabel();
        txtGroupName = new JTextField();
        label2 = new JLabel();
        btnUpdate = new JButton();
        btnCancle = new JButton();
        comCreator = new JComboBox();

        //======== this ========
        setTitle("\u4fee\u6539\u7fa4\u7ec4");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u7fa4\u7ec4\u540d");
        contentPane.add(label1);
        label1.setBounds(new Rectangle(new Point(80, 40), label1.getPreferredSize()));
        contentPane.add(txtGroupName);
        txtGroupName.setBounds(155, 33, 115, txtGroupName.getPreferredSize().height);

        //---- label2 ----
        label2.setText("\u521b\u5efa\u8005");
        contentPane.add(label2);
        label2.setBounds(new Rectangle(new Point(80, 107), label2.getPreferredSize()));

        //---- btnUpdate ----
        btnUpdate.setText("\u4fee\u6539");
        btnUpdate.addActionListener(e -> btnUpdate(e));
        contentPane.add(btnUpdate);
        btnUpdate.setBounds(new Rectangle(new Point(75, 165), btnUpdate.getPreferredSize()));

        //---- btnCancle ----
        btnCancle.setText("\u53d6\u6d88");
        btnCancle.addActionListener(e -> btnCancle(e));
        contentPane.add(btnCancle);
        btnCancle.setBounds(new Rectangle(new Point(200, 165), btnCancle.getPreferredSize()));
        contentPane.add(comCreator);
        comCreator.setBounds(155, 105, 115, comCreator.getPreferredSize().height);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(400, 300);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - yzx
    private JLabel label1;
    private JTextField txtGroupName;
    private JLabel label2;
    private JButton btnUpdate;
    private JButton btnCancle;
    private JComboBox comCreator;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
